package com.jsz.peini.controller.app;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.service.advertise.AdvertiseService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
public class AdvertiseController {
	private static final Logger logger = LoggerFactory.getLogger(AdvertiseController.class);
	@Resource
	private  AdvertiseService advertiseService;
	
	/**
	 * 获取广告列表
	 * @param request
	 * @param adType
	 * @return
	 */
	@ApiOperation("广告列表")
	@RequestMapping(value="/getAdvertise",method=RequestMethod.GET)
	@ResponseBody
	public Object getAdvertise(HttpServletRequest request,@ApiParam(required=true) @RequestParam("adType")Integer adType){
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try{
			if(adType == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			List<LinkedHashMap<String,Object>> advertiseList=advertiseService.getAdvertise(adType);
			for (LinkedHashMap<String, Object> adMap : advertiseList) {
				String url = adMap.get("adLink").toString();
				if(StringUtils.isNotBlank(url)){
					if(url.contains("?")){
						url += "&userId=";
					}else{
						url += "?userId=";
					}
				}
				adMap.put("adLink", url);
			}
			infoMap.put("advertiseList",advertiseList);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
}
